﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊排班计划
    /// </summary>
    public class OtpSchdPlanService : OutpatientAppService, IOtpSchdPlanService
    {
        private readonly IOtpSchdPlanRepository _optSchdPlanRepository;
        private readonly OtpSchdManager _otpSchdManager;
        private readonly IOtpRgstTypeService _otpRgstTypeService;

        public OtpSchdPlanService(IOtpSchdPlanRepository optSchdPlanRepository, OtpSchdManager otpSchdManager
            , IOtpRgstTypeService otpRgstTypeService)
        {
            _optSchdPlanRepository = optSchdPlanRepository;
            _otpSchdManager = otpSchdManager;
            _otpRgstTypeService = otpRgstTypeService;
        }

        /// <summary>
        /// 获取门诊排班计划
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<OtpSchdPlanDto>> GetListAsync(OtpSchdPlanInput input)
        {
            var iQuery = await _optSchdPlanRepository.GetListAsync();

            if (input.Pi == 0)
            {
                var items = iQuery.ToList();
                var itemsDto = ObjectMapper.Map<List<OtpSchdPlan>, List<OtpSchdPlanDto>>(items.ToList());

                return new PagedResultDto<OtpSchdPlanDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<OtpSchdPlan>, List<OtpSchdPlanDto>>(items.ToList());

                return new PagedResultDto<OtpSchdPlanDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        ///获取门诊排班计划
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<OtpSchdPlanDto> GetAsync(Guid id)
        {
            var item = await _optSchdPlanRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<OtpSchdPlan, OtpSchdPlanDto>(item);

            if (itemDto == null)
            {
                itemDto = new OtpSchdPlanDto();
            }

            return itemDto;
        }

        /// <summary>
        /// 修改门诊排班计划信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task PutAsync(Guid id, OtpSchdPlanUpdateDto input)
        {
            var schedulePlan = await _optSchdPlanRepository.GetAsync(id);
            ObjectMapper.Map(input, schedulePlan);
            await _optSchdPlanRepository.UpdateAsync(schedulePlan);
        }

        /// <summary>
        /// 创建门诊排班计划
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<OtpSchdPlanDto> PostAsync(OtpSchdPlanCreateDto input)
        {
            var clinicSchedulePlan = ObjectMapper.Map<OtpSchdPlanCreateDto, OtpSchdPlan>(input);
            var otpRgstType = await _otpRgstTypeService.GetAsync(input.OtpRgstTypeId);
            clinicSchedulePlan.SetOtpRgstType(otpRgstType.OtpRgstTypeName);
            //clinicSchedulePlan = _otpSchdManager.CreateClinicSchedulePlan(clinicSchedulePlan);
            clinicSchedulePlan = await _optSchdPlanRepository.InsertAsync(clinicSchedulePlan);

            OtpSchdPeriod otpSchdPeriod = new OtpSchdPeriod();
            otpSchdPeriod.Week = EnumWeek.星期一;
            otpSchdPeriod.OtpPeriod = EnumOtpPeriod.全天;
            otpSchdPeriod.AmPeriodCnt = 10;
            otpSchdPeriod.PmPeriodCnt = 10;
            otpSchdPeriod.EvPeriodCnt = 0;
            clinicSchedulePlan.OtpSchdPeriods = new List<OtpSchdPeriod> { otpSchdPeriod };

            var itemDto = ObjectMapper.Map<OtpSchdPlan, OtpSchdPlanDto>(clinicSchedulePlan);

            return itemDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task GenerateAsync(Guid id)
        {
            await _otpSchdManager.GenerateOtpSchdRec(id);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid id)
        {
            await _optSchdPlanRepository.DeleteAsync(id);
        }
    }
}
